﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;


namespace HMS.Data
{
    public class UserService
    {
        public dynamic ValidateUser(string accessCode)
        {
            HMSEntities dt = new HMSEntities();
            bool isDoctor = accessCode.Split('-')[1].ToUpper() == "D" ? true : false;
            int userId = 0;
            int clinicId = 0;
            string userName = string.Empty;
            if (isDoctor)
            {
                var doctors = dt.Doctors.Where(x => x.Doc_Access_Code == accessCode);
                if (doctors.Count() > 0) { 
                    var user = doctors.FirstOrDefault();
                    userId = user.Doc_Id;
                    userName = user.Doc_Name;
                    //var docLoc = dt.Doctor_Location.Where(x => x.Doc_Id == userId).FirstOrDefault();
                    //var loc = dt.Locations.Where(x => x.Loc_Id == docLoc.Loc_Id).FirstOrDefault();
                    string clinicCode = accessCode.Split('-')[0].ToUpper();
                    Clinic clinic = dt.Clinics.Where(x => x.Clinic_Code == clinicCode).FirstOrDefault();
                    if (clinic != null)
                        clinicId = clinic.Clinic_Id;
                }
            }
            else
            {
                var patients = dt.Patients.Where(x => x.Patient_Access_Code == accessCode);
                if (patients.Count() > 0) { 
                    userId = patients.FirstOrDefault().Patient_id;
                    userName = patients.FirstOrDefault().Patient_Name;
                    clinicId = patients.FirstOrDefault().Patient_Clinic_Id;
                }
            }
            dynamic userInfo = new System.Dynamic.ExpandoObject();
            userInfo.userId = userId;
            userInfo.userType = isDoctor ? "D" : "P";
            userInfo.ClinicId = clinicId;
            userInfo.UserName = userName;
            return userInfo;
        }
    }
}
